package com.example.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.entity.User;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 * @Author: JohnCoding
 * @Description:
 * @date: 2019/10/31 9:17
 */
public class TokenUtils {

    public static void main(String[] args) {
//        User user = new User();
//        user.setId(1);
//        user.setName("test");
//        user.setPassWord("123456");
//        String token = getToken(user);
//        System.out.println("token == " + token);
        JSONObject object = new JSONObject();
        object.put("id",1);
        object.put("userName","ahaha");
        User user = new User();
        user.setName(object.getString("userName"));
        System.out.println(user);
    }

    /**
     * 获取Token
     *
     * @param user
     * @return
     */
    public static String getToken(User user) {
        String token;
        Date enddate = new Date(System.currentTimeMillis() + 3600L);
        token = JWT.create().withClaim("username", user.getName()).withIssuedAt(new Date()).withExpiresAt(enddate).sign(Algorithm.HMAC256(user.getPassWord()));
        return token;
    }
}
